package HTTP;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


//get请求
public class GetHttp {

    public static void main(String[] args) {
        getHttp();
    }


    public static void getHttp(){
        String urlString = "https://api.oioweb.cn/api/common/OneFilm";

        try {
            // 创建URL对象
            URL url = new URL(urlString);
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法为GET
            connection.setRequestMethod("GET");
            // 获取响应码
            int responseCode = connection.getResponseCode();
            System.out.println("响应码: " + responseCode);
            // 如果响应码为200，则获取响应内容
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 获取Content-Type头信息并解析字符编码
                String contentType = connection.getContentType();
                String charset = "UTF-8"; // 默认字符编码
                if (contentType != null) {
                    Pattern pattern = Pattern.compile("charset=([^;]*)");
                    Matcher matcher = pattern.matcher(contentType);
                    if (matcher.find()) {
                        charset = matcher.group(1);
                    }
                }

                // 创建输入流读取服务器的响应
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName(charset)));
                String inputLine;

                // 创建一个StringBuffer对象用于存储响应内容
                StringBuffer response = new StringBuffer();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                // 关闭输入流
                in.close();
                // 输出响应结果
                System.out.println(response.toString());
            } else {
                System.out.println("请求失败");
            }

            // 断开连接
            connection.disconnect();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //携带参数的get请求
    public  static void getHttp2(){
        try {
            String param1 = "value1";
            String param2 = "value2";
            String urlString = String.format("https://api.oioweb.cn/api/common/OneFilm?param1=%s&param2=%s",
                    URLEncoder.encode(param1, "UTF-8"),
                    URLEncoder.encode(param2, "UTF-8"));

            // 创建URL对象
            URL url = new URL(urlString);

            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法为GET
            connection.setRequestMethod("GET");

            // 获取响应码
            int responseCode = connection.getResponseCode();

            System.out.println("响应码: " + responseCode);

            // 如果响应码为200，则获取响应内容
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 获取Content-Type头信息并解析字符编码
                String contentType = connection.getContentType();
                String charset = "UTF-8";
                if (contentType != null) {
                    int index = contentType.indexOf("charset=");
                    if (index != -1) {
                        charset = contentType.substring(index + 8);
                    }
                }

                // 创建输入流读取服务器的响应
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), charset));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }

                // 关闭输入流
                in.close();

                // 输出响应结果
                System.out.println(response.toString());
            } else {
                System.out.println("请求失败");
            }

            // 断开连接
            connection.disconnect();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
